﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UberChess.Structure.Move;
using UberChess.Structure.Pieces;

namespace UberChess.StateGenerator
{
    [Serializable]
    public class StateGenerator : IStateGenerator
    {
        public List<State> GeneratePossibleStates(State currentState, Side side)
        {
            var possibleMoves = new List<PossibleMove>();
            var possibleStates = new List<State>();
            var pieces = currentState.GetPieces().Where(piece => piece.GetSide() == side).ToList();

            pieces.ForEach(piece => possibleMoves.AddRange(piece.GetPossibleMoves(currentState)));
            possibleMoves.ForEach(move => possibleStates.Add(GenerateNewState(move, currentState)));

            return possibleStates.Where(state => !state.CheckForCheck(side)).ToList();
        }

        private State GenerateNewState(PossibleMove possibleMove, State currentState)
        {
            var newState = (State)Copier.Copy(currentState);
            if(possibleMove.isCaptureMove)
            {
                return newState.CapturePiece(possibleMove, ExchangeType.Queen);
            }
            return newState.MovePiece(possibleMove, ExchangeType.Queen);
        }


    }
}
